package cn.com.spotty.sqldemo.mybatis;

import cn.com.spotty.sqldemo.bean.News;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class MybatisApp {
	public static void main(String[] args) {
		try {
			String resource = "SqlMapConfig.xml";
			InputStream inputStream = Resources.getResourceAsStream(resource);
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
			SqlSession session = factory.openSession();

			NewsMapper mapper = session.getMapper(NewsMapper.class);
			List<News> news = mapper.getNews();
			for (News item : news) {
				System.out.print("标题:" + item.getNewsTitle());
				System.out.print("描述:" + item.getNewsDesc());
				System.out.print("\n");
			}
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
